using System;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.JScript;

namespace JEvaluator {
    public class Evaluator {
        private static object _evaluator = null;
        private static Type _evaluatorType = null;

        public object Evaluate(object[] input, string functionName) {
            return _evaluatorType.InvokeMember(
            functionName,
            BindingFlags.InvokeMethod,
            null,
            _evaluator,
            input
         );

        }

        public Evaluator(string jScript, string assemblyType) {
            ICodeCompiler compiler = new JScriptCodeProvider().CreateCompiler();

            CompilerParameters parameters = new CompilerParameters();
            parameters.GenerateInMemory = true;

            CompilerResults results = compiler.CompileAssemblyFromSource(parameters, jScript);

            Assembly assembly = results.CompiledAssembly;
            _evaluatorType = assembly.GetType(assemblyType);

            _evaluator = Activator.CreateInstance(_evaluatorType);
        }
    }
}

